package com.jxc.jxc01.grc.dao.impl;

import com.jxc.jxc01.grc.dao.IReturnDao;
import com.jxc.jxc01.grc.entity.JDBCConn;
import com.jxc.jxc01.grc.entity.Return;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ReturnDaoImpl implements IReturnDao {
    @Override
    public boolean addOneOfReturn(Return returnModel) {
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc",
//                    "grc", "123456");
            String sql = "INSERT INTO returns(procurement,chargebacks,quantity," +
                    "unit_price,price,states,return_time,cause) VALUES(?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt=JDBCConn.connect(sql);
            pstmt.setInt(1, returnModel.getProcurement());
            pstmt.setInt(2, returnModel.getChargebacks());
            pstmt.setInt(3, returnModel.getQuantity());
            pstmt.setDouble(4, returnModel.getUnit_price());
            pstmt.setDouble(5, returnModel.getPrice());
            pstmt.setString(6, returnModel.getState());
            pstmt.setString(7, returnModel.getReturn_time());
            pstmt.setString(8, returnModel.getCause());
            int i = pstmt.executeUpdate();
            if (i > 0){
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public List<Return> queryReturn() {
        List<Return> list = new ArrayList<>();
        try {
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc",
//                    "grc", "123456");
            String sql = "SELECT * FROM returns";
            PreparedStatement pstmt=JDBCConn.connect(sql);
            ResultSet re = pstmt.executeQuery();
            while (re.next()) {
                Return v = new Return();
                v.setId(re.getInt("id"));
                v.setProcurement(re.getInt("procurement"));
                v.setChargebacks(re.getInt("chargebacks"));
                v.setQuantity(re.getInt("quantity"));
                v.setUnit_price(re.getDouble("unit_price"));
                v.setPrice(re.getDouble("price"));
                v.setState(re.getString("states"));
                v.setReturn_time(re.getString("return_time"));
                v.setCause(re.getString("cause"));
                list.add(v);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getCount() {
        try {
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc",
//                    "grc", "123456");
            String sql = "SELECT COUNT(*) FROM returns";
            PreparedStatement pstmt=JDBCConn.connect(sql);
            ResultSet re = pstmt.executeQuery();
            while (re.next()) {
                int anInt = re.getInt(1);
                return anInt;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    @Override
    public List<Return> queryReturnById(int procurement) {
        List<Return> list = new ArrayList<>();
        try {
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc", "grc", "123456");
            String sql = "SELECT * FROM returns WHERE procurement=?";
            PreparedStatement pstmt=JDBCConn.connect(sql);
            pstmt.setInt(1, procurement);
            ResultSet re = pstmt.executeQuery();
            while (re.next()) {
                Return v = new Return();
                v.setId(re.getInt("id"));
                v.setProcurement(re.getInt("procurement"));
                v.setChargebacks(re.getInt("chargebacks"));
                v.setQuantity(re.getInt("quantity"));
                v.setUnit_price(re.getDouble("unit_price"));
                v.setPrice(re.getDouble("price"));
                v.setState(re.getString("states"));
                v.setReturn_time(re.getString("return_time"));
                v.setCause(re.getString("cause"));
                list.add(v);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
